package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.converter.CatCategoriesConverter;
import com.cskaoyan.shopping.dal.entitys.ItemCat;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dto.CategoriesResponse;
import com.cskaoyan.shopping.service.Categories;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoriesImpl implements Categories {
    @Autowired
    ItemCatMapper itemCatMapper;
    @Autowired
    CatCategoriesConverter catCategoriesConverter;
    @Override
    public CategoriesResponse getCategoriesList() {
        CategoriesResponse categoriesResponse = new CategoriesResponse();
        List<com.cskaoyan.shopping.dal.entitys.Categories> categoriesResponse1 =new ArrayList<>();
        try {
            List<ItemCat> itemCats = itemCatMapper.selectAll();


            for (ItemCat itemcat :
                    itemCats) {
                com.cskaoyan.shopping.dal.entitys.Categories categories = catCategoriesConverter.item2Dto(itemcat);
                categoriesResponse1.add(categories);

            }

            categoriesResponse.setCategoriesResponse(categoriesResponse1);
            categoriesResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            categoriesResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(categoriesResponse, e);
        }

        return categoriesResponse;
    }
}
